package com.hngy.xpq.android_practice.merchant_main.Comm;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.hngy.xpq.android_practice.R;
import com.hngy.xpq.android_practice.bean.Comm;
import com.hngy.xpq.android_practice.database.SQLiteComm;
import com.hngy.xpq.android_practice.http.TcpClient;
import com.hngy.xpq.android_practice.utils.CommOrder;

import org.w3c.dom.Text;

import java.io.ByteArrayOutputStream;
import java.util.List;

public class CommMerchantActivity extends AppCompatActivity {

    ImageButton image;
    EditText name1, price, des;
    TextView name2;
    Spinner type,comm_status;
    SharedPreferences merchant;
    Button bin;
    RadioButton kc, ts, xc;
    SQLiteComm sqLiteComm;
    List<Comm> list;
    //获取美食参考图片变量
    int images;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comm_enter_merchant);

        image = findViewById(R.id.image);
        name1 = findViewById(R.id.comm_name);
        name2 = findViewById(R.id.merchant_comm_name);
        price = findViewById(R.id.comm_price);
        des = findViewById(R.id.comm_des);
        bin = findViewById(R.id.comm_bin);
        type = findViewById(R.id.comm_types);
        comm_status = findViewById(R.id.comm_status);

        sqLiteComm = new SQLiteComm(this);
        merchant = super.getSharedPreferences("merchants", MODE_PRIVATE);
        name2.setText("商家名:  " + merchant.getString("name", ""));

        initData();

        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CommMerchantActivity.this,Comm_image.class);
                startActivityForResult(intent,0x13);
            }
        });
        bin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = getIntent();
                if (TextUtils.isEmpty(name1.getText())) {
                    IndexToast("请输入商品名!!");
                } else if (TextUtils.isEmpty(price.getText())) {
                    IndexToast("请定义价格!!");
                } else {
                    Comm c = new Comm();
                    c.setComm_name(name1.getText().toString());
                    c.setMerchant_name(merchant.getString("name", ""));
                    if (!type.getSelectedItem().toString().equals("请选择食品类型:")){
                        c.setComm_types(type.getSelectedItem().toString());
                    } else {
                        IndexToast("请选择商品类型!!");
                        return;
                    }
                    c.setComm_price(price.getText().toString());
                    c.setComm_phone(merchant.getString("phone", ""));
                    c.setComm_time(CommOrder.getTimeDate());
                    c.setComm_des(des.getText().toString());
                    c.setStatus(comm_status.getSelectedItem().toString());

                    //将商品信息传到服务端

                    if (intent1.getStringExtra("name") == null){
                        byte[] a = img(images);
                        c.setImage(a);
                        String information = "ZC003//" + c.getComm_name() + "//" + c.getMerchant_name()
                                + "//" + c.getComm_types() + "//" + c.getComm_price() + "//" + c.getStatus();
                        String as = new TcpClient().Transits(information);
                        if (as.equals("上架成功") && sqLiteComm.Comm_add(c)) {
                            finish();
                        } else {
                            IndexToast("商品上架失败");
                        }
                    } else {
                        String information = "XG003//" + c.getComm_name() + "//" + c.getMerchant_name()
                                + "//" + c.getComm_types() + "//" + c.getComm_price() + "//" + c.getStatus();
                        String as = new TcpClient().Transits(information);
                        if (as.equals("商品修改成功") && sqLiteComm.updateData(c)){
                            Intent intent = new Intent(CommMerchantActivity.this,Comm_query.class);
                            startActivity(intent);
                            finish();
                        } else {
                            IndexToast("商品修改失败");
                        }
                    }

                }
            }
        });
    }

    private void initData() {    //信息修改界面
        sqLiteComm = new SQLiteComm(this);
        String[] strings = getResources().getStringArray(R.array.comm_types);
        Intent intent = getIntent();
        if (intent != null){
            String names = intent.getStringExtra("merchant_name");
            String status = intent.getStringExtra("status");
            if (intent.getStringExtra("name") != null){

                bin.setText("修改");
                name1.setText(intent.getStringExtra("name"));
                String types = intent.getStringExtra("types");
                int index_types = 0;
                //类型筛选显示
                for (int i = 1 ; i < type.getAdapter().getCount() ; i++){
                    if (strings[i].equals(types)){
                        index_types = i ;
                    }
                }
                type.setSelection(index_types);
                price.setText(intent.getStringExtra("price"));
                des.setText(intent.getStringExtra("des"));
                if (status.equals("上架中")){
                    comm_status.setSelection(0);
                } else if (status.equals("已售尽")){
                    comm_status.setSelection(1);
                } else {
                    comm_status.setSelection(2);
                }
            }
        }
    }

    private void IndexToast(String index) {
        Toast.makeText(CommMerchantActivity.this, index, Toast.LENGTH_SHORT).show();
    }

    public byte[] img(int id) {        //将图片转换成字节
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(id)).getBitmap();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 0x15){
            Bundle bundle = data.getExtras();
            int icon=bundle.getInt("icon");
            ImageView iv= findViewById(R.id.image);
            images = icon;
            iv.setImageResource(icon);
        }
    }
}